jun.NotaDebetDetilGrid = Ext.extend(Ext.grid.GridPanel, {
    title: "NotaDebetDetil",
    id: 'docs-jun.NotaDebetDetilGrid',
    iconCls: "silk-grid",
    viewConfig: {
        forceFit: true
    },
    sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
    columns: [
        {
            header: 'Kode Konsumen',
            sortable: true,
            resizable: true,
            dataIndex: 'konsumen_id',
            width: 100,
            renderer: jun.renderKodeKonsumen
        },
        {
            header: 'Nama Konsumen',
            sortable: true,
            resizable: true,
            dataIndex: 'konsumen_id',
            width: 100,
            renderer: jun.renderNamaKonsumen
        },
        {
            header: 'Lembar',
            sortable: true,
            resizable: true,
            dataIndex: 'lembar',
            width: 100,
            align: "right",
            renderer: Ext.util.Format.numberRenderer("0,0")
        },
        {
            header: 'Nilai',
            sortable: true,
            resizable: true,
            dataIndex: 'nilai',
            width: 100,
            align: "right",
            renderer: Ext.util.Format.numberRenderer("0,0")
        },
        {
            header: 'Total',
            sortable: true,
            resizable: true,
            dataIndex: 'amount',
            width: 100,
            align: "right",
            renderer: Ext.util.Format.numberRenderer("0,0")
        }
    ],
    initComponent: function () {
        this.store = jun.rztNotaDebetDetil;
        this.tbar = {
            xtype: 'toolbar',
            items: [
                {
                    xtype: 'buttongroup',
                    columns: 6,
                    defaults: {
                        scale: 'small'
                    },
                    items: [
                        {
                            text: 'Konsumen :'
                        },
                        {
                            xtype: 'combo',
                            fieldLabel: 'Konsumen',
                            typeAhead: true,
                            triggerAction: 'all',
                            lazyRender: true,
                            mode: 'local',
                            minChars: 3,
                            store: jun.rztKonsumen,
                            matchFieldWidth: !1,
                            itemSelector: "div.search-item",
                            tpl: new Ext.XTemplate('<tpl for="."><div class="search-item">',
                                '<span style="float:left;width:120;font-weight: bold">{konsumen_code}</span>' +
                                '<span style="font-weight: bold">{konsumen_name}</span>', "</div></tpl>"),
                            listWidth: 350,
                            hiddenName: 'konsumen_id',
                            valueField: 'konsumen_id',
                            forceSelection: true,
                            hideTrigger: true,
                            displayField: 'konsumen_name',
                            ref: '../../konsumen',
                            width: 260,
                            allowBlank: false,
                            colspan: 5
                        },
                        {
                            text: 'Lembar:'
                        },
                        {
                            xtype: 'numericfield',
                            id: 'lembarid',
                            ref: '../../lembar',
                            value: 0,
                            width: 45,
                            minValue: 0
                        },
                        {
                            text: 'Nilai:'
                        },
                        {
                            xtype: 'numericfield',
                            id: 'nilaiid',
                            ref: '../../nilai',
                            value: 0,
                            width: 75,
                            minValue: 0
                        },
                        {
                            text: 'Total:'
                        },
                        {
                            xtype: 'numericfield',
                            id: 'totaldetilnotadebetid',
                            readOnly: true,
                            ref: '../../total',
                            value: 0,
                            width: 75,
                            minValue: 0
                        }
                    ]
                },
                {
                    xtype: 'buttongroup',
                    columns: 2,
                    defaults: {
                        scale: 'large'
                    },
                    items: [
                        {
                            xtype: 'button',
                            text: 'Tambah',
                            height: 44,
                            ref: '../../btnAdd'
                        },
                        {
                            xtype: 'button',
                            text: 'Hapus',
                            height: 44,
                            ref: '../../btnDelete'
                        }
                    ]
                }
            ]
        };
        this.store.baseParams = {mode: "grid"};
        this.store.reload();
        this.store.baseParams = {};
        jun.NotaDebetDetilGrid.superclass.initComponent.call(this);
        this.lembar.on('change', this.calculate, this);
        this.nilai.on('change', this.calculate, this);
        this.btnAdd.on('Click', this.loadForm, this);
        this.btnDelete.on('Click', this.deleteRec, this);
        this.store.on('add', this.onStoreChange, this);
        this.store.on('edit', this.onStoreChange, this);
        this.store.on('remove', this.onStoreChange, this);
        this.getSelectionModel().on('rowselect', this.getrow, this);
    },
    onStoreChange: function () {
        jun.rztNotaDebetDetil.refreshData();
    },
    calculate: function(){
        var lembar = parseFloat(this.lembar.getValue());
        var nilai = parseFloat(this.nilai.getValue());
        this.total.setValue(Math.round(lembar * nilai));
    },
    getrow: function (sm, idx, r) {
        this.record = r;
        var selectedz = this.sm.getSelections();
    },
    loadForm: function () {
        this.btnAdd.setDisabled(true);
        var lembar = parseFloat(this.lembar.getValue());
        var nilai = parseFloat(this.nilai.getValue());
        var amount = parseFloat(this.total.getValue());
        var konsumen_id = this.konsumen.getValue();
        if (konsumen_id == "") {
            Ext.MessageBox.alert("Error", "Konsumen harus dipilih.");
            this.btnAdd.setDisabled(false);
            return
        }
//        var a = jun.rztNotaDebetDetil.findExact("konsumen_id", konsumen_id);
//        if (a > -1) {
//            Ext.MessageBox.alert("Error", "Konsumen sudah dimansukkan.");
//            this.btnDisabled(false);
//            return;
//        }
        var c = jun.rztNotaDebetDetil.recordType,
            d = new c({
                konsumen_id: konsumen_id,
                lembar: lembar,
                nilai: nilai,
                amount: amount
            });
        jun.rztNotaDebetDetil.add(d);
        this.lembar.reset();
        this.nilai.reset();
        this.total.reset();
        this.konsumen.reset();
        this.btnAdd.setDisabled(false);
    },
    loadEditForm: function () {
        var selectedz = this.sm.getSelected();
        //var dodol = this.store.getAt(0);
        if (selectedz == undefined) {
            Ext.MessageBox.alert("Warning", "Anda belum memilih Jenis Pelayanan");
            return;
        }
        var idz = selectedz.json.nota_debet_detil_id;
        var form = new jun.NotaDebetDetilWin({modez: 1, id: idz});
        form.show(this);
        form.formz.getForm().loadRecord(this.record);
    },
    deleteRec: function () {
        Ext.MessageBox.confirm('Pertanyaan', 'Apakah anda yakin ingin menghapus data ini?', this.deleteRecYes, this);
    },
    deleteRecYes: function (btn) {
        if (btn == 'no') {
            return;
        }
        var record = this.sm.getSelected();
        if (record == undefined) {
            Ext.MessageBox.alert("Warning", "Anda belum memilih item.");
            return;
        }
        this.store.remove(record);
    }
})
